package com.wangcang.widgetlibrary.activity;

import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wangcang.widgetlibrary.R;
import com.wangcang.widgetlibrary.activity.base.BaseActivity;
import com.wangcang.widgetlibrary.tools.ConvertUtils;

import java.util.ArrayList;

public class CardDeskRefreshActivity extends BaseActivity {

    ImageView backBt = null;
    LinearLayout loaddingLayout = null;
    TextView titleText = null;
    ArrayList<String> listData = null; // 数据源\
    GridView gridView = null;
    Handler handler = null;
    GridAdapter adapter = null;
    LayoutInflater mInflater = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_card_desk_refresh);

        backBt = findView(R.id.back_bt);
        loaddingLayout = findView(R.id.loadding_layout);
        titleText = findView(R.id.title_text);
        gridView = findView(R.id.gridview);
        mInflater = LayoutInflater.from(this);
        handler = new Handler();
        listData = new ArrayList<>();
        titleText.setText("卡片桌面");

        backBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finishActivity();
            }
        });

        // 初始化grid数据
        for (int i = 0; i < 3; i++) {
            listData.add("第" + i + "个卡片");
        }

        adapter = new GridAdapter();
        gridView.setAdapter(adapter);

        loaddingLayout.setVisibility(View.VISIBLE);
        titleText.setVisibility(View.GONE);
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                listData.add("第3张卡片");
                listData.add("第4张卡片");
                adapter.notifyDataSetChanged();
                loaddingLayout.setVisibility(View.GONE);
                titleText.setVisibility(View.VISIBLE);
            }
        }, 3000);
    }

    class GridAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return listData.size();
        }

        @Override
        public Object getItem(int position) {
            return listData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        ViewHolder holder = null;
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            if (convertView == null) {
                holder = new ViewHolder();
                convertView = mInflater.inflate(R.layout.carddesk_grid_item, null);
                holder.itemLayout = (LinearLayout) convertView.findViewById(R.id.item_layout);
                holder.textView = (TextView) convertView.findViewById(R.id.text);
                convertView.setTag(holder);
            }
            else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.textView.setText(listData.get(position));
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) holder.itemLayout.getLayoutParams();
            params.height = (getScreenParams().getHeight() - ConvertUtils.dip2px(mContext, 120)) / 3;
            holder.itemLayout.setLayoutParams(params);
            holder.itemLayout.setBackgroundColor(getResources().getColor(R.color.common_color_c7_96999e));
            return convertView;
        }

        class ViewHolder {
            TextView textView = null;
            LinearLayout itemLayout = null;
        }
    }
}
